package com.example.cd5160866.modelrecycleview.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cd5160866.modelrecycleview.R;
import com.example.cd5160866.modelrecycleview.adapter.ModelRecyclerAdapterV3;
import com.example.cd5160866.modelrecycleview.adapter.RecyclerItemViewId;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cd5160866 on 2016/10/8.
 */
public class Demo2Activity extends DemoBaseActivity {
    @BindView(R.id.rv_demo_base)
    RecyclerView recyclerView;
    ModelRecyclerAdapterV3 adapter;

    private List<View> listHeader = new ArrayList<>();

    private List<View> listFooter = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter = new ModelRecyclerAdapterV3(this, MyImageViewHolder.class, data);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_demo2, menu);
        return true;
    }

    //    private int idsNumber = 0;

    private TextView getTestView() {
        //        idsNumber++;
        TextView view = (TextView) View.inflate(this, R.layout.item_header_text, null);
        //        view.setId(idsNumber);
        //        view.setText(view.getId() + "");
        return view;
    }

    /**
     * 我们的实际使用中的viewhoder
     * 注解注入item的布局文件，在这个类的实现中，我们可以绑定点击事件，更新数据
     */
    @RecyclerItemViewId(R.layout.item_list)
    public static class MyImageViewHolder extends ModelRecyclerAdapterV3.ModelViewHolder<String> {
        @BindView(R.id.iv_item1)
        ImageView imageView;

        /**
         * 可以对itemview的任何一个view绑定监听，这里只是以onclick为例，当然也可以绑定onTouch,onLongClick等
         */
        @OnClick(R.id.iv_item1)
        void onclick() {
            Toast.makeText(imageView.getContext(), position + " 点击~", Toast.LENGTH_SHORT).show();
        }

        public int position;


        public MyImageViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void bindButterKnife(View convertView) {
            ButterKnife.bind(this, convertView);
        }

        @Override
        public void update(String item, ModelRecyclerAdapterV3 adapter, int position) {
            this.position = position;
            Picasso.with(adapter.getContext()).load(item).into(imageView);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        LinearLayoutManager manager;
        switch (item.getItemId()) {
            case R.id.list_vertical:
                manager = new LinearLayoutManager(this);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                break;
            case R.id.list_horizontal:
                manager = new LinearLayoutManager(this);
                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                recyclerView.setLayoutManager(manager);
                break;
            case R.id.gridview:
                manager = new GridLayoutManager(this, 4);
                recyclerView.setLayoutManager(manager);
                break;
            case R.id.add_header:
                TextView header = getTestView();
                listHeader.add(0, header);
                int id = adapter.addHeaderView(header);
                header.setText(id + "");
                break;
            case R.id.remove_header:
                if (listHeader.size() > 0) {
                    View view = listHeader.get(0);
                    adapter.removeHeaderView(view);
                    listHeader.remove(view);
                }
                break;
            case R.id.add_footer:
                TextView footer = getTestView();
                footer.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                listFooter.add(0, footer);
                int idFooter = adapter.addFooterView(footer);
                footer.setText(idFooter + "");
                break;
            case R.id.remove_footer:
                if (listFooter.size() > 0) {
                    View view = listFooter.get(0);
                    adapter.removeFooterView(view);
                    listFooter.remove(view);
                }
                break;
            default:
                break;
        }
        return true;
    }

}
